package com.xj.until;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xj.pojo.User;
import org.springframework.stereotype.Component;

@Component
public class Useres {

    public User userss(String jsonString){

        // 创建Jackson的ObjectMapper对象
        ObjectMapper objectMapper = new ObjectMapper();

        // 将JSON字符串解析为JsonNode对象
        JsonNode jsonNode = null;
        try {
            jsonNode = objectMapper.readTree(jsonString);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        User user = new User();
        // 从JsonNode对象中获取特定字段的值
        String email = jsonNode.get("email").asText();
        String name = jsonNode.get("name").asText();
        String phone = jsonNode.get("phone").asText();
        String unit = jsonNode.get("unit").asText();
        int id = jsonNode.get("id").asInt();
        String username = jsonNode.get("username").asText();
        String rolecode = jsonNode.get("rolecode").asText();
        user.setEmail(email);
        user.setName(name);
        user.setPhone(phone);
        user.setId(id);
        user.setUsername(username);
        user.setUnit(unit);
        user.setRolename(rolecode);
        return user;
    }
}
